
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'examination/paper/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 30, key: true },
            { label: '试卷名', name: 'name', index: 'name', width: 80 },
			{ label: '考试时间', name: 'time', index: 'time', width: 30 },
			{ label: '题目数量', name: 'questionsCount', width: 30 },
			{ label: '描述', name: 'description', index: 'description', width: 120 },
			{ label: '封面图片', name: 'coverPath', index: 'cover_path', width: 50,
				formatter: function (data) {
					return `<img src="${data}" style="height: 40px;width: 40px"></img>`;

				} },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
		multiboxonly:true,
		//beforeSelectRow: beforeSelectRow,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

	new AjaxUpload('#upload', {
		action: baseURL + "course/teacher/upload",
		name: 'file',
		autoSubmit:true,
		responseType:"json",
		onSubmit:function(file, extension){
			if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
				alert('只支持jpg、png、gif格式的图片！');
				return false;
			}
		},
		onComplete : function(file, r){
			if(r.code == 0){
				$("#pic").attr('src',r.returnUrl);
				vm.examinationPaper.coverPath=r.returnUrl
			}else{
				alert(r.msg);
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: 11,
		title: null,
		examinationPaper: {},
		examinationResultType: {},
		examinationQuestions: {},
		resultList: []
	},
	methods: {
		query: function () {
			vm.reload1();
		},
		add1: function(){
			vm.showList = 12;
			vm.title = "新增";
			vm.examinationPaper = {};
		},
		update1: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = 12;
            vm.title = "修改";
            
            vm.getInfo1(id)
		},
		saveOrUpdate1: function (event) {
			if (vm.examinationPaper.name == null) {
				alert("试卷名不能为空");
				return ;
			}
			if (vm.examinationPaper.time == null) {
				alert("考试时间不能为空");
				return ;
			}
			var url = vm.examinationPaper.id == null ? "examination/paper/save" : "examination/paper/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.examinationPaper),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload1();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del1: function (event) {
			var id = getSelectedRows();
			if(id == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "examination/paper/delete",
                    contentType: "application/json",
				    data: JSON.stringify(id),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo1: function(id){
			$.get(baseURL + "examination/paper/info/"+id, function(r){
                vm.examinationPaper = r.examinationPaper;
            });
		},
		reload1: function (event) {
			vm.showList = 11;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},

		resultType: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = 21;
			$("#jqGrid2").jqGrid({
				url: baseURL + 'examination/resulttype/list/'+id,
				datatype: "json",
				colModel: [
					{ label: 'ID', name: 'id', index: 'id', width: 40, key: true },
					{ label: '所属试卷', name: 'paperId', index: 'paper_id', width: 80 },
					{ label: '类型', name: 'typename', index: 'typename', width: 100 },
					{ label: '类型描述', name: 'description', index: 'description', width: 760 },
				],
				viewrecords: true,
				height: 385,
				rowNum: 10,
				rowList : [10,30,50],
				rownumbers: true,
				rownumWidth: 25,
				autowidth:true,
				multiselect: true,
				multiboxonly:true,
				//beforeSelectRow: beforeSelectRow2,
				pager: "#jqGridPager2",
				jsonReader : {
					root: "page.list",
					page: "page.currPage",
					total: "page.totalPage",
					records: "page.totalCount"
				},
				prmNames : {
					page:"page",
					rows:"limit",
					order: "order"
				},
				gridComplete:function(){
					//隐藏grid底部滚动条
					$("#jqGrid2").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
				}
			});
		},
		add2: function(){
			vm.showList = 22;
			vm.title = "新增";
			vm.examinationResultType = {paperId:getSelectedRow()};
		},
		update2: function (event) {
			var id = getSelectedRow2();
			if(id == null){
				return ;
			}
			vm.showList = 22;
			vm.title = "修改";

			vm.getInfo2(id)
		},
		saveOrUpdate2: function (event) {
			if (vm.examinationResultType.typename == null) {
				alert("结果类型名不能为空");
				return ;
			}
			var url = vm.examinationResultType.id == null ? "examination/resulttype/save" : "examination/resulttype/update";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.examinationResultType),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload2();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del2: function (event) {
			var id = getSelectedRows2();
			if(id == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
					url: baseURL + "examination/resulttype/delete",
					contentType: "application/json",
					data: JSON.stringify(id),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid2").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo2: function(id){
			$.get(baseURL + "examination/resulttype/info/"+id, function(r){
				vm.examinationResultType = r.examinationResultType;
			});
		},
		return21: function (event) {
			vm.showList = 11;
			var parent=$("#jqg2"); // 获得整个表格容器
			parent.empty();
			$("<table id='jqGrid2'></table>").appendTo(parent);
			$("<div id='jqGridPager2'></div>").appendTo(parent);
		},
		reload2: function (event) {
			vm.showList = 21;
			var page = $("#jqGrid2").jqGrid('getGridParam','page');
			$("#jqGrid2").jqGrid('setGridParam',{
				page:page
			}).trigger("reloadGrid");
		},
		questions: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = 31;
			getResultList();
			$("#jqGrid3").jqGrid({
				url: baseURL + 'examination/questions/list/'+id,
				datatype: "json",
				colModel: [
					{ label: 'ID', name: 'id', index: 'id', width: 40, key: true },
					{ label: '所属试卷', name: 'paperId', index: 'paper_id', width: 80 },
					{ label: '问题', name: 'question', index: 'question', width: 400 },
					{ label: '选项1', name: 'option1', index: 'option1', width: 80 },
					{ label: '选项2', name: 'option2', index: 'option2', width: 80 },
					{ label: '选项3', name: 'option3', index: 'option3', width: 80 },
					{ label: '选项4', name: 'option4', index: 'option4', width: 80 },
					{ label: '选项5', name: 'option5', index: 'option5', width: 80 },
				],
				viewrecords: true,
				height: 385,
				rowNum: 10,
				rowList : [10,30,50],
				rownumbers: true,
				rownumWidth: 25,
				autowidth:true,
				multiselect: true,
				multiboxonly:true,
				//beforeSelectRow: beforeSelectRow3,
				pager: "#jqGridPager3",
				jsonReader : {
					root: "page.list",
					page: "page.currPage",
					total: "page.totalPage",
					records: "page.totalCount"
				},
				prmNames : {
					page:"page",
					rows:"limit",
					order: "order"
				},
				gridComplete:function(){
					//隐藏grid底部滚动条
					//$("#jqGrid2").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
				}
			});
		},
		add3: function(){
			vm.showList = 32;
			vm.title = "新增";
			vm.examinationQuestions = {paperId:getSelectedRow()};
		},
		update3: function (event) {
			var id = getSelectedRow3();
			if(id == null){
				return ;
			}
			vm.showList = 32;
			vm.title = "修改";

			vm.getInfo3(id)
		},
		saveOrUpdate3: function (event) {
			if (vm.examinationQuestions.question == null) {
				alert("题目不能为空");
				return ;
			}
			if (vm.examinationQuestions.option1 == null) {
				alert("选项1不能为空");
				return ;
			}
			if (vm.examinationQuestions.option1ResultTypeId == null) {
				alert("选项1倾向不能为空");
				return ;
			}
			if (vm.examinationQuestions.option1Score == null) {
				alert("选项1分值不能为空");
				return ;
			}
			if (vm.examinationQuestions.option2 == null) {
				alert("选项2不能为空");
				return ;
			}
			if (vm.examinationQuestions.option2ResultTypeId == null) {
				alert("选项2倾向不能为空");
				return ;
			}
			if (vm.examinationQuestions.option2Score == null) {
				alert("选项2分值不能为空");
				return ;
			}
			if (vm.examinationQuestions.option3 != null) {
				if (vm.examinationQuestions.option3ResultTypeId == null) {
					alert("必须为不为空的选项设置倾向");
					return ;
				}
				if (vm.examinationQuestions.option3Score == null) {
					alert("必须为不为空的选项设置分值");
					return ;
				}
			}
			if (vm.examinationQuestions.option4 != null) {
				if (vm.examinationQuestions.option4ResultTypeId == null) {
					alert("必须为不为空的选项设置倾向");
					return ;
				}
				if (vm.examinationQuestions.option4Score == null) {
					alert("必须为不为空的选项设置分值");
					return ;
				}
			}
			if (vm.examinationQuestions.option5 != null) {
				if (vm.examinationQuestions.option5ResultTypeId == null) {
					alert("必须为不为空的选项设置倾向");
					return ;
				}
				if (vm.examinationQuestions.option5Score == null) {
					alert("必须为不为空的选项设置分值");
					return ;
				}
			}
			var url = vm.examinationQuestions.id == null ? "examination/questions/save" : "examination/questions/update";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.examinationQuestions),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload3();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del3: function (event) {
			var id = getSelectedRows3();
			if(id == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
					url: baseURL + "examination/questions/delete",
					contentType: "application/json",
					data: JSON.stringify(id),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid3").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo3: function(id){
			$.get(baseURL + "examination/questions/info/"+id, function(r){
				vm.examinationQuestions = r.examinationQuestions;
			});
		},
		return31: function (event) {
			vm.showList = 11;
			var parent=$("#jqg3"); // 获得整个表格容器
			parent.empty();
			$("<table id='jqGrid3'></table>").appendTo(parent);
			$("<div id='jqGridPager3'></div>").appendTo(parent);
		},
		reload3: function (event) {
			vm.showList = 31;
			var page = $("#jqGrid3").jqGrid('getGridParam','page');
			$("#jqGrid3").jqGrid('setGridParam',{
				page:page
			}).trigger("reloadGrid");
		},
	}
});
function beforeSelectRow() {
	$("#jqGrid").jqGrid('resetSelection');
	return (true);
}
function beforeSelectRow2() {
	$("#jqGrid2").jqGrid('resetSelection');
	return (true);
}
function beforeSelectRow3() {
	$("#jqGrid3").jqGrid('resetSelection');
	return (true);
}
//选择一条记录
function getSelectedRow2() {
	var grid = $("#jqGrid2");
	var rowKey = grid.getGridParam("selrow");
	if(!rowKey){
		alert("请选择一条记录");
		return ;
	}

	var selectedIDs = grid.getGridParam("selarrrow");
	if(selectedIDs.length > 1){
		alert("只能选择一条记录");
		return ;
	}

	return selectedIDs[0];
}
function getSelectedRow3() {
	var grid = $("#jqGrid3");
	var rowKey = grid.getGridParam("selrow");
	if(!rowKey){
		alert("请选择一条记录");
		return ;
	}

	var selectedIDs = grid.getGridParam("selarrrow");
	if(selectedIDs.length > 1){
		alert("只能选择一条记录");
		return ;
	}

	return selectedIDs[0];
}
function getSelectedRows2() {
	var grid = $("#jqGrid2");
	var rowKey = grid.getGridParam("selrow");
	if(!rowKey){
		alert("请选择一条记录");
		return ;
	}

	return grid.getGridParam("selarrrow");
}
function getSelectedRows3() {
	var grid = $("#jqGrid3");
	var rowKey = grid.getGridParam("selrow");
	if(!rowKey){
		alert("请选择一条记录");
		return ;
	}

	return grid.getGridParam("selarrrow");
}
function getResultList() {
	var paperId = getSelectedRow();
	$.ajax({
		type: "POST",
		url: baseURL + "examination/resulttype/getResultList/"+paperId,
		contentType: "application/json",
		success: function(r){
			console.log(r)
			vm.resultList = r.page.list;
		}
	});
}